@IsTest
public with sharing class John_Test_Record_Delete {
    

    @IsTest
    static void testDeleteByDML(){
        // Account acc = new Account(Name = '宝安钢铁有限公司');
        // insert acc;

        // Contact contact = new Contact(LastName = '张', FirstName = '三', AccountId = acc.Id);
        // insert contact;

        // Account result = [SELECT Id, Name, (SELECT FirstName, LastName FROM Contacts) FROM Account WHERE Id = :acc.Id];
        // System.assertEquals('宝安钢铁有限公司', result.Name);
        // System.assertEquals('三', result.getSObjects('Contacts')[0].get('FirstName'));  // 两个对象都已保存成功，并且存在关联关系

        // Test.startTest();
        // delete acc;
        // List<Account> accList = [SELECT Id, Name FROM Account WHERE Id = :acc.Id];
        // List<Contact> contactList = [SELECT Id, Name FROM Contact WHERE Id = :contact.Id];
        // Test.stopTest();
        
        // System.assertEquals(0, accList.size());  // Account已被删除
        // System.assertEquals(0, contactList.size());  // 关联该Account的子对象Contact也被删除

        User u = [SELECT Id, Name From User WHERE username = 'lijun0372@playful-otter-7wxsf7.com' limit 1];

        Test.startTest();
        System.runAs(u){
            Teacher__c teacher = new Teacher__c(Name= 'John');
            insert teacher;

            Student__c stu = new Student__c(Student_Name__c = '小王', School_Fee__c = 198, Teacher__c = teacher.Id);
            insert stu;

            // 验证Lookup关系，删除主对象是否从对象会被自动删除
           /*  List<Teacher__c> teaList = [SELECT Id,Name FROM Teacher__c WHERE Name= 'John' LIMIT 1];
            List<Student__c> stuList = [SELECT Id,Name,Student_Name__c FROM Student__c WHERE Student_Name__c = '小王' LIMIT 1];
            System.assert(teaList.size() > 0);
            System.assert(stuList.size() > 0); */
            
            List<Teacher__c> teaList = [SELECT Id, Name  FROM Teacher__c WHERE Name= 'John' LIMIT 1];
            System.assert(teaList.size() > 0);
            System.assertEquals('John', teaList[0].Name);
            // System.assertEquals('小王', teaList[0].TeacherName__r.get(0).Student_Name__c);
            
            /* delete teacher;
            List<Student__c> stuListAfterDel = [SELECT Id,Name,Student_Name__c FROM Student__c WHERE Student_Name__c = '小王' LIMIT 1];
            System.assertEquals(0, stuListAfterDel.size()); */


            // 子查询查询相关列表的写法， 从结果集中getSObjects的写法
            // SOQL返回值： List<sObject>、sObject


        }
        Test.stopTest();

        

    }

}